package cn.jiangjiesheng.bootstrap.cloud.service;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
//import com.qycloud.service.core.Client;
//import cn.jiangjiesheng.bootstrap.qpaas.legacy.PlatformService;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;
//import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.context.annotation.Configuration;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * Created by dev@jiangjiesheng.cn on 2022/11/25  <br/>
 *
 * @author dev@jiangjiesheng.cn
 */
@RefreshScope
@Data
@Configuration
@ConditionalOnMissingBean(ProjectMetadataProperties.class)
@ConfigurationProperties(prefix = "project-metadata")
public class ProjectMetadataProperties {

//    @Autowired
//    private PlatformService platformService;

    private String domain;

    private String internalDomain;

    private Map<String, List<String>> entUserMap = Maps.newConcurrentMap();

    private List<String> entIdList;

    public List<String> getEntAdminUserId(String entId) {
        if (entUserMap.containsKey(entId)) {
            return entUserMap.get(entId);
        }
        synchronized (this) {
            if (entUserMap.containsKey(entId)) return this.getEntAdminUserId(entId);
//            final List<String> adminUser = platformService.getAdminUsers(entId);
            entUserMap.put(entId, Lists.newArrayList("admin_mock"));
        }
        return entUserMap.get(entId);
    }

    /**
     * 校验容器ID是否配置正确
     *
     * @return 错误ID列表
     */
    public List<String> checkEntId() {

        List<String> metadataErrorList = new ArrayList<>();
        if (CollUtil.isNotEmpty(entIdList)) {
            String resultStr = "['ent_mock','ent_mock']";//Client.getString("admin.enterprise.getEnterpriseList");
            if (StrUtil.isNotBlank(resultStr)) {
                List<String> entIds = JSONUtil.toList(resultStr, String.class);
                metadataErrorList = entIdList.stream().filter(v -> !entIds.contains(v)).collect(Collectors.toList());
            }
        }
        return metadataErrorList;
    }


    /**
     * 获取配置的容器id集合中的第一个
     *
     * @return 容器id
     * @author wangxudong
     * @date 2023/8/3 8:25
     */
    public String getEntId() {
        if (CollUtil.isEmpty(entIdList)) {
            return null;
        }
        return entIdList.get(0);
    }

    /**
     * 获取配置的容器id集合中的第一个对应的第一个容器管理员id
     *
     * @return 用户id
     * @author wangxudong
     * @date 2023/8/3 8:26
     */
    public String getUserId() {
        String entId = this.getEntId();
        if (StringUtils.isBlank(entId)) {
            return null;
        }
        List<String> adminUserIds = this.getEntAdminUserId(entId);
        if (CollUtil.isEmpty(adminUserIds)) {
            return null;
        }
        return adminUserIds.get(0);
    }
}
